--
-- Author: yawei.zhu
-- Date: 2018-10-08 15:13:12
--

local PanelSpacing = 3
local LineSpacing = 8
local UPSpacing = 8
local DownSpacing = 6

local TextWidth = 290

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.WorldBossTips))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._panel = self._ui.Panel
	self._panelBG = self._ui.bg

	self._panel:setTouchEnabled(true)
	self._width = self._panel:getContentSize2()
end


-- data是一个数组，代表文本
-- 每一个value的信息：
-- string 文本，不能为空
-- font  字体大小，可以为空
-- color  字体颜色，cc.c4b, 可以为空
function ClassRef:refresh()
	if not self._data or #self._data <= 0 then 
		self._panel:setVisible(false)
		return 
	end
	self._panel:setVisible(true)
	local datas = self._data
	-- 清理掉所有的child（除了bg）
	local childrens = self._panel:getChildren()
	for _, child in pairs(childrens) do
		if child ~= self._panelBG then
			child:removeFromParent()
		end
	end

	local posY = DownSpacing
	local function setText(index, data)
		local color = data.color or Enums.QualityColor2[1]
		local ttfConfig = {}
   		ttfConfig.fontFilePath = Res.font
    	ttfConfig.fontSize = data.font or 18
    	local label = cc.Label:createWithTTF(ttfConfig, data.string, cc.TEXT_ALIGNMENT_LEFT)
    	label:setTextColor(color)
		label:setMaxLineWidth(TextWidth)
		label:setAnchorPoint(cc.p(0, 1))
		label:addTo(self._panel)

		local height = label:getContentSize().height
		posY = posY + height
		label:setPosition(cc.p(12.5, posY))

		if index ~= 1 then
			posY = posY + LineSpacing
			-- 添加line
			local line = ccui.ImageView:create()
			line:loadTexture("art/home/common_tip_line.png")
			line:setAnchorPoint(cc.p(0.5, 1))
			line:setPosition(cc.p(self._width / 2, posY))
			line:addTo(self._panel)

			posY = posY + PanelSpacing
		else
			posY = posY + UPSpacing
		end
	end

	local count = #datas
	for i = count, 1, -1 do
		local data = datas[i]
		setText(i, data)
	end

	self:setContentSize(cc.size(self._width, posY))
	self._panel:setContentSize(cc.size(self._width, posY))
	self._panelBG:setContentSize(cc.size(self._width, posY))
	self._panelBG:setPositionY(posY)
end

return ClassRef
